package com.wanlian.candy.controller;

import com.wanlian.common.PageCondition;
import com.wanlian.entity.Task;
import com.wanlian.entity.User;
import com.wanlian.entity.UserTask;
import com.wanlian.json.JsonResult;
import com.wanlian.json.JsonStringUtil;
import com.wanlian.model.ResultModel;
import com.wanlian.service.TaskServices;
import com.wanlian.service.TaskTimerServices;
import com.wanlian.service.UserServices;
import com.wanlian.service.UserTaskServices;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;


/**
 * 用户完成任务时，可以获取积分或者int
 */
@Controller
@RequestMapping("/task")
public class TaskController {
    @Autowired
    private UserServices userServices;

    @Autowired
    private TaskServices taskServices;

    @Autowired
    private UserTaskServices userTaskServices;

    @Autowired
    private TaskTimerServices timerServices;


    /**
     * 查询某台设备的积分历史
     *
     * @param data
     * @return
     */
    @RequestMapping(value = "/score", method = {RequestMethod.POST}, produces = "application/json")
    @ResponseBody
    public String queryUserScore(@RequestBody String data) {
        User user = JsonStringUtil.readValue(data, User.class);
        ResultModel resultModel = userServices.queryUserTotalScore(user.getId());
        return new JsonResult().Serailze(resultModel);
    }


    /**
     * 查询某个用户积分任务
     *
     * @param data
     * @return
     */
    @RequestMapping(value = "/user/list/id", method = {RequestMethod.POST}, produces = "application/json")
    @ResponseBody
    public String addUserTask(@RequestBody String data) {
        User user = JsonStringUtil.readValue(data, User.class);
        ResultModel resultModel = userServices.queryUserTotalScore(user.getId());
        return new JsonResult().Serailze(resultModel);
    }

    /**
     * 添加每日积分任务
     *
     * @param data
     * @return
     */
    @RequestMapping(value = "/add", method = {RequestMethod.POST}, produces = "application/json")
    @ResponseBody
    public String addTaskScore(@RequestBody String data) {
        Task task = JsonStringUtil.readValue(data, Task.class);
        ResultModel resultModel = taskServices.doAdd(task);
        return new JsonResult().Serailze(resultModel);
    }

    /**
     * 修改积分任务
     *
     * @param data
     * @return
     */
    @RequestMapping(value = "/update", method = {RequestMethod.POST}, produces = "application/json")
    @ResponseBody
    public String modifName(@RequestBody String data) {
        Task task = JsonStringUtil.readValue(data, Task.class);
        ResultModel resultModel = taskServices.doUpdateById(task);
        return new JsonResult().Serailze(resultModel);
    }

    /**
     * 删除积分任务
     *
     * @param data
     * @return
     */
    @RequestMapping(value = "/del", method = {RequestMethod.POST}, produces = "application/json")
    @ResponseBody
    public String delTasks(@RequestBody String data) {
        Task task = JsonStringUtil.readValue(data, Task.class);
        ResultModel resultModel = taskServices.doDeleteById(task);
        return new JsonResult().Serailze(resultModel);
    }

    /**
     * 启用积分任务
     *
     * @param data
     * @return
     */
    @RequestMapping(value = "/enable", method = {RequestMethod.POST}, produces = "application/json")
    @ResponseBody
    public String enableTasks(@RequestBody String data) {
        Task task = JsonStringUtil.readValue(data, Task.class);
        ResultModel resultModel = taskServices.doEnableById(task);
        return new JsonResult().Serailze(resultModel);
    }



    /**
     * 按Id查询任务
     *
     * @param data
     * @return
     */
    @RequestMapping(value = "/info", method = {RequestMethod.POST}, produces = "application/json")
    @ResponseBody
    public String queryTasksById(@RequestBody String data) {
        Task task = JsonStringUtil.readValue(data, Task.class);
        int id = task.getId();
        Task tasks = taskServices.queryTasksById(id);
        ResultModel resultModel = new ResultModel(true, tasks);
        return new JsonResult().Serailze(resultModel);
    }


    /**
     * 删除用户积分任务
     *
     * @param data
     * @return
     */
    @RequestMapping(value = "/user/del", method = {RequestMethod.POST}, produces = "application/json")
    @ResponseBody
    public String delUserTasks(@RequestBody String data) {
        UserTask userTask = JsonStringUtil.readValue(data, UserTask.class);
        ResultModel resultModel = userTaskServices.delUserTask(userTask);
        return new JsonResult().Serailze(resultModel);
    }


    /**
     * 添加用户积分任务
     *
     * @param data
     * @return
     */
    @RequestMapping(value = "/user/add", method = {RequestMethod.POST}, produces = "application/json")
    @ResponseBody
    public String addUserTasks(@RequestBody String data) {
        UserTask userTask = JsonStringUtil.readValue(data, UserTask.class);
        ResultModel resultModel = userTaskServices.addUserTask(userTask);
        return new JsonResult().Serailze(resultModel);
    }


    /**
     * 查询每日积分任务
     *
     * @param data
     * @return
     */
    @RequestMapping(value = "/list", method = {RequestMethod.POST}, produces = "application/json")
    @ResponseBody
    public String listTasks(@RequestBody String data) {
        PageCondition conditions = JsonStringUtil.readValue(data, PageCondition.class);
        ResultModel resultModel = taskServices.queryTasks(conditions);
        return new JsonResult().Serailze(resultModel);
    }

    /**
     * 查询用户积分任务
     *
     * @param data
     * @return
     */
    @RequestMapping(value = "/user/list", method = {RequestMethod.POST}, produces = "application/json")
    @ResponseBody
    public String listUserTasks(@RequestBody String data) {
        PageCondition conditions = JsonStringUtil.readValue(data, PageCondition.class);
        ResultModel resultModel = userTaskServices.queryUserTasks(conditions);
        return new JsonResult().Serailze(resultModel);
    }

    /**
     * 查询全部用户任务
     *
     * @return
     */
    @RequestMapping(value = "/user/listall", method = {RequestMethod.POST}, produces = "application/json")
    @ResponseBody
    public String listAllUserTask(@RequestBody String data) {
        PageCondition pageCondition = JsonStringUtil.readValue(data, PageCondition.class);

        ResultModel resultModel = taskServices.queryAllUserTask(pageCondition);
        System.out.println(new JsonResult().Serailze(resultModel));
        return new JsonResult().Serailze(resultModel);
    }


//    /**
//     * 查询全部用户任务
//     *
//     * @return
//     */
//    @RequestMapping(value = "/send/int", method = {RequestMethod.POST}, produces = "application/json")
//    @ResponseBody
//    public String sendInt() {
//        timerServices.doEdit();
//        return new JsonResult().Serailze(true);
//    }

}
